package com.universal.schedule;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.universal.schedule.db.DBManager;
import com.universal.schedule.db.DatabaseHelper;
import com.universal.schedule.timetable.CourseModel;

public class UpdateScheduleActivity extends Activity {

	private EditText mEditText1;
	private EditText mEditText2;
	private TextView mTv5;
	private TextView mTv7;
	private TextView mTv9;
	private CourseModel mCourseModel;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_update_schedule);
		findView();
		initUI();
		initData();
	}

	private void initData() {
		mEditText1.setText(mCourseModel.getCourseName());
		mEditText2.setText(mCourseModel.getTeacherName());
		mTv5.setText(mCourseModel.getWeek()+"");
		mTv7.setText(mCourseModel.getSection()+"-"+(mCourseModel.getSectionSpan()-1)+"节");
		mTv9.setText(mCourseModel.getClassRoom());
	}

	private void initUI() {
		mCourseModel = (CourseModel) getIntent().getSerializableExtra("schedule");

	}

	private void findView() {
		mEditText1 = (EditText) findViewById(R.id.editText1);
		mEditText2 = (EditText) findViewById(R.id.editText2);
		mTv5 = (TextView) findViewById(R.id.textView5);
		mTv7 = (TextView) findViewById(R.id.textView7);
		mTv9 = (TextView) findViewById(R.id.textView9);
		TextView mPublic = (TextView) findViewById(R.id.edit_public);

		mPublic.setOnClickListener(new OnClickListener() {

			private String whereClause=DatabaseHelper.COLUMN_SCHEDULE_ID;

			@Override
			public void onClick(View v) {
				ContentValues cv=new ContentValues();
				cv.put(DatabaseHelper.COLUMN_SCHEDULE_COURSEName, mEditText1.getText().toString());
				cv.put(DatabaseHelper.COLUMN_SCHEDULE_TEACHERNAME, mEditText2.getText().toString());
				DBManager.getInstance(UpdateScheduleActivity.this).updateById(DatabaseHelper.SCHEDULE_TABLE, whereClause, String.valueOf(mCourseModel.getId()),cv);
				mCourseModel.setTeacherName(mEditText2.getText().toString());
				mCourseModel.setCourseName(mEditText1.getText().toString());
				Toast.makeText(UpdateScheduleActivity.this, "修改成功", Toast.LENGTH_LONG).show();
				Intent data=new Intent();
				data.putExtra("schedule", mCourseModel);
				setResult(1, data);
				finish();

			}
		});

	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		switch (item.getItemId()) {
			case android.R.id.home:
				finish();
				break;

			default:
				break;
		}
		return super.onOptionsItemSelected(item);
	}
}
